package widgets;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wenxin.administrator.zhufengfm.R;

import java.security.PrivateKey;

/**
 * Created by Administrator on 15-10-22.
 */

/**
 * 用于在发现模块中 推栏目中
 */
public class SpecialItemView extends RelativeLayout {
    /**
     * 在代码中使用new SpecialItemView()
     * @param context
     */
    public SpecialItemView(Context context) {
        super(context);
    }

    private ImageView imageView;
    private TextView txtTitle;
    private TextView txtSubTitle;
    private TextView txtNnumber;
    private ImageView imageArrow;
    private ImageView imageLine;
    /**
     * 在布局文件中使用控件中 使用的构造
     *
     * @param context
     * @param attrs
     */
    public SpecialItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //这样就可以同时在代码中使用
        init(context,attrs);
    }

    public void init(Context context,AttributeSet attrs){
        //初始化控件
        imageView=new ImageView(context);
        //手写属性,layoutParams代表在布局中android：layout_XXX
        //控件要添加到哪一个容器中就要用他的LayoutParams
        RelativeLayout.LayoutParams lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
                );
        //相当于layout_centerVertical="true"
        lp.addRule(CENTER_VERTICAL);
        imageView.setId(R.id.special_item_icon);
        imageView.setLayoutParams(lp);
        imageView.setImageResource(R.mipmap.ic_launcher);
        addView(imageView);
        //-------------------------------------------
        //添加标题

        txtTitle=new TextView(context);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        //设置txtTitle和ImageView顶部对其
        lp.addRule(ALIGN_TOP,R.id.special_item_icon);
        lp.leftMargin=20;
        txtTitle.setLayoutParams(lp);
        txtTitle.setText("标题");
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtTitle.setTextColor(Color.BLACK);
        txtTitle.setSingleLine();
        addView(txtTitle);
        //-------------------------------------------
         txtSubTitle=new TextView(context);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        lp.addRule(CENTER_VERTICAL);
        lp.addRule(ALIGN_LEFT, R.id.special_item_subtitle);
        txtSubTitle.setLayoutParams(lp);
        txtSubTitle.setText("Subtitle");
        txtSubTitle.setSingleLine();
        addView(txtSubTitle);
        //-------------------------------------------
        //数量
        txtNnumber=new TextView(context);
        txtNnumber.setId(R.id.special_item_number);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        //设置对其方式，与图片底部对其和标题左对齐
        lp.addRule(ALIGN_BOTTOM, R.id.special_item_icon);
        lp.addRule(ALIGN_LEFT,R.id.special_item_title);
        txtNnumber.setLayoutParams(lp);
        txtNnumber.setText("10首声音");
        //设置图片
        Drawable drawable= ContextCompat.getDrawable(context,R.mipmap.search_album);
        drawable.setBounds(0, 0, 20, 20);
        txtNnumber.setCompoundDrawables(drawable, null, null, null);
        addView(txtNnumber);
       //-------------------------------------------
        imageArrow=new ImageView(context);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(CENTER_VERTICAL);
        imageArrow.setImageResource(R.mipmap.arrow_right);
        imageArrow.setLayoutParams(lp);
        addView(imageArrow);
        //-------------------------------------------
         imageLine=new ImageView(context);
        lp=new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                1
        );
        lp.addRule(ALIGN_LEFT,R.id.recommend_album_title);
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(BELOW,R.id.special_item_number);
        lp.setMargins(0,4,0,0);
        imageLine.setLayoutParams(lp);
        imageLine.setImageResource(R.drawable.shape_line);
        addView(imageLine);
    }
    public void setTxtTitle(String title){

        txtTitle.setText(title);
    }
    public void setTxtSubTitle(String subTitle){
        txtSubTitle.setText(subTitle);
    }
    public void setNumber(String number){
        txtNnumber.setText(number);
    }
    public void setShowBottomLine(boolean show) {
        if (show){
            imageLine.setVisibility(VISIBLE);
        }else{
            imageLine.setVisibility(INVISIBLE);
        }
    }
    public void setRightArrowResource(int resId){
        imageArrow.setImageResource(resId);
    }
}
